package com.cyq.day21.managementsystem;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class LoginFrame extends JFrame {
    private JPanel p1, p2, p3;
    private JTextField textField;
    private JPasswordField passwordField;
    private JButton btn1, btn2;
    private JLabel label1, label2;
    int index;

    public LoginFrame(Employee[] employees) {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(200, 100, 500, 500);
        setTitle("登录");
        setLayout(new GridLayout(3, 1, 5, 5));
        p1 = new JPanel();
        label1 = new JLabel();
        label1.setText("用户名：");
        textField = new JTextField(20);
        p1.add(label1);
        p1.add(textField);
        //
        p2 = new JPanel();
        label2 = new JLabel();
        label2.setText("密 码：");
        passwordField = new JPasswordField(20);
        p2.add(label2);
        p2.add(passwordField);
        //
        p3 = new JPanel();
        btn1 = new JButton("登录");
        btn2 = new JButton("取消");
        p3.add(btn1);
        p3.add(btn2);
        this.add(p1);
        this.add(p2);
        this.add(p3);
        pack();
        btn1.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String username = textField.getText();
                String pass = new String(passwordField.getPassword());

                if (username.equals("")) {
                    JOptionPane.showMessageDialog(null, "用户名不能为空！");
                    textField.grabFocus();
                    return;
                } else if (pass.equals("")) {
                    JOptionPane.showMessageDialog(null, "密码不能为空！");
                    passwordField.grabFocus();
                    return;
                }
                for (int i = 0; i < employees.length; i++) {
                    if (employees[i] != null) {
                        if (username.equals(employees[i].getName())
                                && pass.equals(employees[i].getPassword())) {
                            JOptionPane.showMessageDialog(null, "恭喜你，可以登录了");
                            index = i;
                            if (employees[i].getLevel().equals("管理员")) {
                                AdminManagement adminManagement = new AdminManagement(employees, index);
                                adminManagement.setVisible(true);
                                LoginFrame.this.dispose();
                                return;
                            } else if (employees[i].getLevel().equals("员工")) {
                                EmployeeManagement employeeManagement = new EmployeeManagement(employees, index);
                                employeeManagement.setVisible(true);
                                LoginFrame.this.dispose();
                                return;
                            }
                        }
                    }
                }
                JOptionPane.showMessageDialog(null, "用户名或者密码错误");
            }
        });
        btn2.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                JOptionPane.showMessageDialog(null, "谢谢使用再见");
                System.exit(0);
            }
        });
    }
}
